<?php

namespace app\api\controller;


use app\api\validation\Music;
use app\api\validation\MusicMenu;
use app\services\MusicService;
use support\Request;

class MusicController
{
    public function add(Request $request)
    {
        try {
            $data = $request->post();

            $validator = new Music();
            if (!$validator->check($data)) {
                throw new \Exception($validator->getError());
            }

            MusicService::addMusic($data);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function show(Request $request)
    {
        try {
            $data = $request->post();

            $data = MusicService::showMusic($data['id']);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function update(Request $request)
    {
        try {
            $data = $request->post();

            $validator = new Music();
            if (!$validator->check($data)) {
                throw new \Exception($validator->getError());
            }

            MusicService::updateMusic($data['id'], $data);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            $data = $request->post();

            MusicService::deleteMusic($data['id']);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addMenu(Request $request)
    {
        try {
            $data = $request->post();

            $validator = new MusicMenu();
            if (!$validator->check($data)) {
                throw new \Exception($validator->getError());
            }

            MusicService::addMusicMenu($data);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function showMenu(Request $request)
    {
        try {
            $data = $request->post();

            $data = MusicService::showMenu($data['id']);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function showBelongMenus(Request $request)
    {
        try {
            $data = $request->post();

            $data = MusicService::showMusicMenus($data['id']);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function updateMenu(Request $request)
    {
        try {
            $data = $request->post();

            $validator = new MusicMenu();
            if (!$validator->check($data)) {
                throw new \Exception($validator->getError());
            }

            MusicService::updateMusicMenu($data['id'], $data);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function deleteMenu(Request $request)
    {
        try {
            $data = $request->post();

            MusicService::deleteMusicMenu($data['id']);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function menus(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword');

            $data = MusicService::musicMenus($page, $length, $keyword);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function musics(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword');

            $data = MusicService::musics($page, $length, $keyword);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function sort(Request $request)
    {
        try {
            $id = $request->post('id');
            $type = $request->post('type');

            if ($type == 'up') {
                MusicService::sortUp($id);
            } else {
                MusicService::sortDown($id);
            }

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addMusicToMenu(Request $request)
    {
        try {
            $data = $request->post();

            if (empty($data['music_id']) || empty($data['menu_id'])) {
                throw new \Exception('乐谱ID或谱单ID不能为空');
            }

            MusicService::addMusicToMenu($data['music_id'], $data['menu_id']);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function removeMusicFromMenu(Request $request)
    {
        try {
            $data = $request->post();

            if (empty($data['music_id']) || empty($data['menu_id'])) {
                throw new \Exception('乐谱ID或谱单ID不能为空');
            }

            MusicService::removeMusicFromMenu($data['music_id'], $data['menu_id']);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function exportToPdf(Request $request)
    {
        try {
            $data = $request->post();

            return ok_rsp('成功', ['url' => MusicService::exportToPdf($data['id'])]);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function musicsFromMenu(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword');
            $menuId = $request->post('menu_id');

            $data = MusicService::musicsFromMenu($menuId, $page, $length, $keyword);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function musicalInstruments()
    {
        try {
            $data = MusicService::getMusicalInstruments();

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }
}